<template>
  <div class="goods-relevant">
    <!-- 头部 -->
    <div class="header">
      <i class="icon" />
      <span class="title">{{ relevant }}</span>
    </div>
    <!-- 底部 -->
    <div class="slider">
      <a href="javascript:;" class="carousel-btn prev" @click="index--"
        ><i class="iconfont icon-angle-left"></i
      ></a>
      <a href="javascript:;" class="carousel-btn next" @click="index++"
        ><i class="iconfont icon-angle-right"></i
      ></a>
      <router-link
        tag="div"
        v-for="goods in sun"
        :key="goods.id"
        :to="`/detail/${goods.id}`"
      >
        <img :src="goods.picture" alt="" />
        <p class="name ellipsis">{{ goods.name }}</p>
        <p class="price">&yen;{{ goods.price }}</p>
      </router-link>
      <div class="carousel-indicator">
        <span
          v-for="i in 4"
          :key="i"
          :class="{ a: index == i - 1 }"
          @click="index = i - 1"
        ></span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: ["relevantlist", "relevant"],
  data() {
    return {
      index: 0,
    };
  },
  computed: {
    sun() {
      let a = this.relevantlist;
      return a.slice(this.index * 4, (this.index + 1) * 4);
    },
  },
  watch: {
    index: {
      handler(val) {
        if (val >= 4) {
          this.index = 0;
        }
        if (val <= -1) {
          this.index = 3;
        }
      },
      deep: true,
      immediate: true,
    },
  },
};
</script>

<style scoped lang="scss">
.carousel-btn {
  width: 44px;
  height: 44px;
  background: rgba(255, 255, 255, 0.2);
  color: #ccc;
  border-radius: 50%;
  position: absolute;
  top: 50%;
  transform: translate(0, -50%);
  z-index: 2;
  text-align: center;
  line-height: 44px;
  transition: all 0.5s;
  &.prev {
    left: 20px;
    i {
      font-size: 30px !important;
    }
  }
  &.next {
    right: 20px;
    i {
      font-size: 30px !important;
    }
  }
}
.a {
  background-color: #27ba9b !important;
}
.carousel-indicator {
  position: absolute;
  left: 50%;
  bottom: -20px;
  transform: translate(-50%, 30px) !important;
  z-index: 2;
  width: 200px;
  text-align: center;
  span {
    display: inline-block;
    width: 12px;
    height: 12px;
    background: rgba(0, 0, 0, 0.2);
    border-radius: 50%;
    cursor: pointer;
    ~ span {
      margin-left: 12px;
    }
  }
}
.slider {
  display: flex;
  justify-content: space-around;
  padding: 30px 40px;
  position: relative !important;
  a {
    text-decoration: none;
  }
  > div {
    width: 240px;
    text-align: center;
    img {
      // padding: 20px;
      width: 190px !important;
      height: 190px !important;
    }
    .name {
      // width: 100%;
      font-size: 16px;
      color: #666;
      padding: 0 40px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    .price {
      font-size: 16px;
      color: red;
      margin-top: 15px;
    }
  }
}
.goods-relevant {
  background: rgb(255, 255, 255);
  // min-height: 460px;
  margin-top: 20px;
  // width: 1240px;
  height: 460px;
  margin: 20px auto;
  .header {
    height: 80px;
    line-height: 80px;
    padding: 0 20px;
    .title {
      font-size: 20px;
      padding-left: 10px;
    }
    .icon {
      width: 16px;
      height: 16px;
      display: inline-block;
      border-top: 4px solid #27ba9b;
      border-right: 4px solid #27ba9b;
      box-sizing: border-box;
      position: relative;
      transform: rotate(45deg);
      &::before {
        content: "";
        width: 10px;
        height: 10px;
        position: absolute;
        left: 0;
        top: 2px;
        background: lighten(#27ba9b, 40%);
      }
    }
  }
}
</style>
